import { request } from '@/utils/request'
import type { ApiResponse } from '@/types/api'

// 主题信息接口
export interface ThemeInfo {
  name: string
  displayName: string
  version: string
  author: string
  description: string
  screenshot: string
  installed: boolean
  active: boolean
  templates: string[]
  config?: Record<string, any>
}

// 主题管理API
export const themeApi = {
  // 获取所有可用主题
  getAvailableThemes(): Promise<ApiResponse<ThemeInfo[]>> {
    return request({
      url: '/admin/theme/list',
      method: 'get',
    })
  },

  // 获取主题信息
  getThemeInfo(themeName: string): Promise<ApiResponse<ThemeInfo>> {
    return request({
      url: `/admin/theme/${themeName}`,
      method: 'get',
    })
  },

  // 安装主题
  installTheme(themeName: string, file: File): Promise<ApiResponse<void>> {
    const formData = new FormData()
    formData.append('themeName', themeName)
    formData.append('file', file)
    
    return request({
      url: '/admin/theme/install',
      method: 'post',
      data: formData,
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    })
  },

  // 卸载主题
  uninstallTheme(themeName: string): Promise<ApiResponse<void>> {
    return request({
      url: `/admin/theme/${themeName}`,
      method: 'delete',
    })
  },

  // 激活主题
  activateTheme(themeName: string, siteId: number): Promise<ApiResponse<void>> {
    return request({
      url: `/admin/theme/${themeName}/activate`,
      method: 'post',
      params: { siteId },
    })
  },

  // 获取主题配置
  getThemeConfig(themeName: string): Promise<ApiResponse<Record<string, any>>> {
    return request({
      url: `/admin/theme/${themeName}/config`,
      method: 'get',
    })
  },

  // 更新主题配置
  updateThemeConfig(themeName: string, config: Record<string, any>): Promise<ApiResponse<void>> {
    return request({
      url: `/admin/theme/${themeName}/config`,
      method: 'put',
      data: config,
    })
  },

  // 获取主题模板列表
  getThemeTemplates(themeName: string): Promise<ApiResponse<string[]>> {
    return request({
      url: `/admin/theme/${themeName}/templates`,
      method: 'get',
    })
  },

  // 获取模板内容
  getTemplateContent(themeName: string, templateName: string): Promise<ApiResponse<string>> {
    return request({
      url: `/admin/theme/${themeName}/template/${templateName}`,
      method: 'get',
    })
  },

  // 更新模板内容
  updateTemplateContent(themeName: string, templateName: string, content: string): Promise<ApiResponse<void>> {
    return request({
      url: `/admin/theme/${themeName}/template/${templateName}`,
      method: 'put',
      data: content,
      headers: {
        'Content-Type': 'text/plain',
      },
    })
  },

  // 复制主题
  copyTheme(sourceTheme: string, targetTheme: string): Promise<ApiResponse<void>> {
    return request({
      url: '/admin/theme/copy',
      method: 'post',
      data: { sourceTheme, targetTheme },
    })
  },

  // 导出主题
  exportTheme(themeName: string): Promise<Blob> {
    return request({
      url: `/admin/theme/${themeName}/export`,
      method: 'get',
      responseType: 'blob',
    })
  },

  // 预览主题
  previewTheme(themeName: string, templateName: string, data: Record<string, any> = {}): Promise<ApiResponse<string>> {
    return request({
      url: `/admin/theme/${themeName}/preview/${templateName}`,
      method: 'post',
      data,
    })
  },

  // 获取主题预览URL
  getPreviewUrl(themeName: string, templateName: string = 'index'): string {
    return `/admin/theme/${themeName}/preview/${templateName}`
  },
}
